﻿using System;
using CodeGenerator.CSharp.Extensions;

namespace CodeGenerator.CSharp
{
    public class Field : CodeUnit
    {
        #region Constructors

        public Field(string name, ModifierCollection modifiers, string dataType)
            : base(name, modifiers)
        { base.DataType = dataType; }

        #endregion

        #region Default Value

        public string DefaultValue { get; set; }
        private bool HasDefaultValue { get { return this.DefaultValue.IsSomething() && this.DefaultValue.Length > 0; } }

        #endregion

        #region Instantiate

        /// <summary>
        /// If the data type of this field is an object, identifies if this object should 
        /// be instantiated.  Should not be used in with DefaultValue.
        /// </summary>
        public bool Instantiate { get; set; }

        #endregion

        #region ToString

        public readonly CodeBuilder x = new CodeBuilder();

        public override string ToString()
        {
            if (this.HasDefaultValue && this.Instantiate) throw new Exception("Cannot set both DefaultValue and Instantiate in a Field: " + base.Name);

            CodeBuilder Code = new CodeBuilder();

            base.AppendDocumentation(Code);
            base.AppendAttributes(Code);

            string InstantiateString = this.Instantiate ? " = new " + this.DataType + "()" : "";
            string DefaultValueString = this.HasDefaultValue ? " = " + this.DefaultValue : "";
            Code.Add("{0}{1} {2} {3}{4}{5};", this.Padding, this.Modifiers, this.DataType, this.Name, InstantiateString, DefaultValueString);

            return Code.ToString();
        }

        #endregion

    }

}
